package com.kayang.dd.controller;

import com.kayang.common.HttpMsg;
import com.kayang.dd.config.DDconfig;
import com.kayang.dd.service.AccessTokenService;
import com.kayang.dd.pojo.DDcontext;
import com.kayang.dd.pojo.PersonalTaskModel;
import com.kayang.dd.pojo.TaskModel;
import com.kayang.dd.pojo.param.UpdateTaskParam;
import com.kayang.dd.service.TaskService;
import com.kayang.nadapt.util.Convert;
import com.kayang.nadapt.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping(value = "/task")
public class TaskController extends BaseController{
    @Autowired
    AccessTokenService tokenService;

    @Autowired
    DDconfig dDconfig;

    @Autowired
    TaskService taskService;

    private String taskPath = "https://api.dingtalk.com";
    @PostMapping("/create")
    public ResponseEntity creatTask(@Valid @RequestBody TaskModel taskModel){
        String url = taskPath + "/v1.0/todo/users/{unionId}/tasks";
        url = url.replace("{unionId}", taskModel.getUnionId());
        if(!StringUtil.isNullOrEmpty(taskModel.getOperatorId())){
           url += "?operatorId={operatorId}".replace("{operatorId}", taskModel.getOperatorId());
        }
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(taskModel.toMap(taskModel));

        Map headers = new HashMap();
        String token = tokenService.getAccessTokenStr(dDconfig.getAppKey()); //TODO
        headers.put("x-acs-dingtalk-access-token", token);

        Map param = taskModel.toMap(taskModel);
        param.remove("unionId");
        param.remove("operatorId");
        Map ret = taskService.creatTask(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    @PostMapping("/createPersonal")
    public ResponseEntity createPersonalTask(@Valid @RequestBody PersonalTaskModel personalTaskModel){
        String url = taskPath + "/v1.0/todo/users/me/personalTasks";
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");
        context.setBody(personalTaskModel.toMap(personalTaskModel));

        Map headers = new HashMap();
        String token = tokenService.getAccessTokenStr(dDconfig.getAppKey()); //TODO
        headers.put("x-acs-dingtalk-access-token", token);

        Map ret = taskService.creatPersonalTask(personalTaskModel, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("创建成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @DeleteMapping("/delete")
    public ResponseEntity deleteTask(@RequestParam String unionId, @RequestParam String taskId, @RequestParam(required = false) String operatorId){
        String url = taskPath + "/v1.0/todo/users/{unionId}/tasks/{taskId}?operatorId={operatorId}";
        url = url.replace("{unionId}", unionId).replace("{taskId}", taskId).replace("{operatorId}", operatorId == null ? "" : operatorId);
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("DELETE");

        Map headers = new HashMap();
        String token = tokenService.getAccessTokenStr(dDconfig.getAppKey());
        headers.put("x-acs-dingtalk-access-token", token);

        Map ret = taskService.deleteTask(context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("删除成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }

    @PutMapping("/put")
    public ResponseEntity updateTask(@Valid @RequestBody UpdateTaskParam updateTaskParam){
        Map param = updateTaskParam.toMap(updateTaskParam);
        String url = taskPath + "/v1.0/todo/users/{unionId}/tasks/{taskId}?operatorId={operatorId}";
        url = url.replace("{unionId}", Convert.toString(param.get("unionId"))).replace("{taskId}", Convert.toString(param.get("taskId"))).replace("{operatorId}", param.get("taskId") == null ? "" : Convert.toString(param.get("taskId")));
        param.remove("unionId");
        param.remove("taskId");
        param.remove("operatorId");
        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("PUT");

        Map headers = new HashMap();
        String token = tokenService.getAccessTokenStr(dDconfig.getAppKey());
        headers.put("x-acs-dingtalk-access-token", token);

        Map ret = taskService.updateTask(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("更新成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }


    /**
     * param :
     * {
     *     "unionId" : "A001", //必填
     *     "nextToken" : "123",
     *     "isDone" : false,
     *     "roleTypes" :[["executor"], ["creator"]]
     * }
     * */
    @PostMapping("/query")
    public ResponseEntity queryTask(@RequestBody Map param){
        String url = taskPath + "/v1.0/todo/users/{unionId}/org/tasks/query";
        url = url.replace("{unionId}", Convert.toString(param.get("unionId")));
        param.remove("unionId");

        DDcontext context = new DDcontext();
        context.setUrl(url);
        context.setMethod("POST");

        Map headers = new HashMap();
        String token = tokenService.getAccessTokenStr(dDconfig.getAppKey());
        headers.put("x-acs-dingtalk-access-token", token);

        Map ret = taskService.updateTask(param, context, headers);
        HttpMsg httpMsg = new HttpMsg();
        httpMsg.setMsgId(0);
        httpMsg.setMsg("查询成功");
        httpMsg.setResult(ret);
        return ResponseEntity.ok(httpMsg);
    }





}
